/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package org.eclipse.iota.model;

import static org.eclipse.iota.model.EquationType.*;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.iota.IotaPackage;
import org.eclipse.iota.util.IotaEqualityUtil;

/**
 * @see org.eclipse.iota.IotaPackage#getEquation()
 * @model kind="class"
 * @generated
 */
public class Equation extends Constraint {
	
	/**
	 * @generated NOT
	 */
	public Equation(Expression left, Expression right, EquationType type) {
		super();
		setLeft(left);
		setRight(right);
		setType(type);
	}
	
	/**
	 * @generated NOT
	 */
	public Equation(Expression left, Expression right) {
		this(left, right, EQUAL);
	}

	/**
	 * @generated NOT
	 */
	@Override
	public boolean isTrue() {
		return type==EQUAL && left!=null && right!=null && IotaEqualityUtil.equals(left, right);
	}

	/**
	 * @generated NOT
	 */
	@Override
	public boolean isFalse() {
		return (type==NOT_EQUAL || type==GREATER || type==LESS) && 
				left!=null && right!=null && IotaEqualityUtil.equals(left, right);
	}

	/**
	 * Flip this equation.
	 * @generated NOT
	 */
	public void flip() {
		Expression left = this.left;
		setLeft(right);
		setRight(left);
		if (type==LESS) {
			setType(GREATER);
		}
		else if (type==GREATER) {	// The 'else' is important!
			setType(LESS);
		}
	}
	
	/**
	 * @generated NOT
	 */
	@Override
	public String toString() {
		String type = this.type.toString();
		if (this.type==EQUAL && !inferred) type = " " + type + " ";
		String result = left + type + right;
		return inferred ? "[" + result + "]" : result;
	}
	
	/* ---------------------------------------------------------------- *
	 * GENERATED CODE.                                                  *
	 * Do not edit below this line. If you need to edit, move it above  *
	 * this line and change the '@generated'-tag to '@generated NOT'.   *
	 * ---------------------------------------------------------------- */	

	/**
	 * The cached value of the '{@link #getLeft() <em>Left</em>}' containment reference.
	 * @see #getLeft()
	 * @generated
	 * @ordered
	 */
	protected Expression left;

	/**
	 * The cached value of the '{@link #getRight() <em>Right</em>}' containment reference.
	 * @see #getRight()
	 * @generated
	 * @ordered
	 */
	protected Expression right;

	/**
	 * The default value of the '{@link #getType() <em>Type</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getType()
	 * @generated
	 * @ordered
	 */
	protected static final EquationType TYPE_EDEFAULT = EquationType.EQUAL;

	/**
	 * The cached value of the '{@link #getType() <em>Type</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getType()
	 * @generated
	 * @ordered
	 */
	protected EquationType type = TYPE_EDEFAULT;

	/**
	 * @generated
	 */
	public Equation() {
		super();
	}

	/**
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return IotaPackage.Literals.EQUATION;
	}

	/**
	 * Returns the value of the '<em><b>Left</b></em>' containment reference.
	 * @return the value of the '<em>Left</em>' containment reference.
	 * @see #setLeft(Expression)
	 * @see org.eclipse.iota.IotaPackage#getEquation_Left()
	 * @model containment="true"
	 * @generated
	 */
	public Expression getLeft() {
		return left;
	}

	/**
	 * @generated
	 */
	public NotificationChain basicSetLeft(Expression newLeft, NotificationChain msgs) {
		Expression oldLeft = left;
		left = newLeft;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, IotaPackage.EQUATION__LEFT, oldLeft, newLeft);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * Sets the value of the '{@link org.eclipse.iota.model.Equation#getLeft <em>Left</em>}' containment reference.
	 * @param value the new value of the '<em>Left</em>' containment reference.
	 * @see #getLeft()
	 * @generated
	 */
	public void setLeft(Expression newLeft) {
		if (newLeft != left) {
			NotificationChain msgs = null;
			if (left != null)
				msgs = ((InternalEObject)left).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - IotaPackage.EQUATION__LEFT, null, msgs);
			if (newLeft != null)
				msgs = ((InternalEObject)newLeft).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - IotaPackage.EQUATION__LEFT, null, msgs);
			msgs = basicSetLeft(newLeft, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, IotaPackage.EQUATION__LEFT, newLeft, newLeft));
	}

	/**
	 * Returns the value of the '<em><b>Right</b></em>' containment reference.
	 * @return the value of the '<em>Right</em>' containment reference.
	 * @see #setRight(Expression)
	 * @see org.eclipse.iota.IotaPackage#getEquation_Right()
	 * @model containment="true"
	 * @generated
	 */
	public Expression getRight() {
		return right;
	}

	/**
	 * @generated
	 */
	public NotificationChain basicSetRight(Expression newRight, NotificationChain msgs) {
		Expression oldRight = right;
		right = newRight;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, IotaPackage.EQUATION__RIGHT, oldRight, newRight);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * Sets the value of the '{@link org.eclipse.iota.model.Equation#getRight <em>Right</em>}' containment reference.
	 * @param value the new value of the '<em>Right</em>' containment reference.
	 * @see #getRight()
	 * @generated
	 */
	public void setRight(Expression newRight) {
		if (newRight != right) {
			NotificationChain msgs = null;
			if (right != null)
				msgs = ((InternalEObject)right).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - IotaPackage.EQUATION__RIGHT, null, msgs);
			if (newRight != null)
				msgs = ((InternalEObject)newRight).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - IotaPackage.EQUATION__RIGHT, null, msgs);
			msgs = basicSetRight(newRight, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, IotaPackage.EQUATION__RIGHT, newRight, newRight));
	}

	/**
	 * Returns the value of the '<em><b>Type</b></em>' attribute.
	 * The literals are from the enumeration {@link org.eclipse.iota.model.EquationType}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Type</em>' attribute.
	 * @see org.eclipse.iota.model.EquationType
	 * @see #setType(EquationType)
	 * @see org.eclipse.iota.IotaPackage#getEquation_Type()
	 * @model
	 * @generated
	 */
	public EquationType getType() {
		return type;
	}

	/**
	 * Sets the value of the '{@link org.eclipse.iota.model.Equation#getType <em>Type</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @param value the new value of the '<em>Type</em>' attribute.
	 * @see org.eclipse.iota.model.EquationType
	 * @see #getType()
	 * @generated
	 */
	public void setType(EquationType newType) {
		EquationType oldType = type;
		type = newType == null ? TYPE_EDEFAULT : newType;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, IotaPackage.EQUATION__TYPE, oldType, type));
	}

	/**
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case IotaPackage.EQUATION__LEFT:
				return basicSetLeft(null, msgs);
			case IotaPackage.EQUATION__RIGHT:
				return basicSetRight(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case IotaPackage.EQUATION__LEFT:
				return getLeft();
			case IotaPackage.EQUATION__RIGHT:
				return getRight();
			case IotaPackage.EQUATION__TYPE:
				return getType();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case IotaPackage.EQUATION__LEFT:
				setLeft((Expression)newValue);
				return;
			case IotaPackage.EQUATION__RIGHT:
				setRight((Expression)newValue);
				return;
			case IotaPackage.EQUATION__TYPE:
				setType((EquationType)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case IotaPackage.EQUATION__LEFT:
				setLeft((Expression)null);
				return;
			case IotaPackage.EQUATION__RIGHT:
				setRight((Expression)null);
				return;
			case IotaPackage.EQUATION__TYPE:
				setType(TYPE_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case IotaPackage.EQUATION__LEFT:
				return left != null;
			case IotaPackage.EQUATION__RIGHT:
				return right != null;
			case IotaPackage.EQUATION__TYPE:
				return type != TYPE_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

}
